package com.example.doctorstation.POJOController;

import com.example.doctorstation.Config.MyResponse;
import com.example.doctorstation.Helper.StringHelper;
import com.example.doctorstation.POJOData.Record;
import com.example.doctorstation.POJOMapper.RecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class RecordController {
    @Autowired
    RecordMapper RecordMapper;

    @GetMapping(value="/record")
    public MyResponse getRecord(Record info) {
        try
        {
            //参数为recordInfoID，优先执行，获取病历内容
            if(info.getRecordInfoID() !=null) {
                return new MyResponse(RecordMapper.getRecordContent(info.getRecordInfoID()), "获取到这份电子病历！", "不存在这份电子病历");
            }
            //参数为patientID，获取该病人的所有电子病历信息（不包含病历内容）
            if(info.getPatientID() !=0 && info.getPatientID() !=-1) {
                return new MyResponse(RecordMapper.getRecordInfo(info.getPatientID()), "查询到该病人的电子病历信息！", "该医院不存在该病人的电子病历信息");
            }
            return new MyResponse();
        }
        catch (Throwable e)
        {
            e.printStackTrace();
            return new MyResponse();
        }
    }

    @PostMapping(value="/record")
    public MyResponse insertRecord(Record info)
    {
        try
        {
            int res = -1;
            RecordMapper.insertRecord(info.getPatientID(),info.getRecordName(),info.getRecordInfoType(),info.getCreateUserID(),info.getContent(),res);
            return new MyResponse(res,"成功增加该病人的病历","增加病历失败");
        }
        catch (Throwable e)
        {
            e.printStackTrace();
            return new MyResponse();
        }
    }

    @PutMapping (value="/record")
    public MyResponse updateRecord(Record info)
    {
        try
        {
            int res = -1;
            RecordMapper.updateRecord(info.getLastUserID(),info.getRecordInfoID(),info.getRecordName(),info.getContent(), res);
            return new MyResponse(res,"成功更新该病人的病历","更新病历失败");
        }
        catch (Throwable e)
        {
            e.printStackTrace();
            return new MyResponse();
        }
    }
}
